package com.hspedu.lambda2;

import org.junit.Test;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 一、构造器引用
 *
 * 二、数组引用
 *
 *
 * Created by shkstart
 */
@SuppressWarnings("all")
public class ConstructorRefTest {
	//构造器引用
    //Supplier中的T get()
    @Test
    public void test1(){
        Supplier<Employee> sup = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee(1001);
            }
        };

        Supplier<Employee> sup2 = () -> new Employee(1002);

        Supplier<Employee> sup3 = Employee::new;
	}

	//Function中的R apply(T t)
    @Test
    public void test2(){
        Function<Integer,Employee> func1 = new Function<Integer, Employee>() {
            @Override
            public Employee apply(Integer id) {
                return new Employee(id);
            }
        };

        Function<Integer, Employee> func2 = id -> new Employee(id);

        Function<Integer, Employee> func3 = Employee::new;
        func3.apply(1002);
	}

	//BiFunction中的R apply(T t,U u)
    @Test
    public void test3(){
        BiFunction<Integer,String,Employee> func1 = (id,name) -> new Employee(id,name);
        BiFunction<Integer,String,Employee> func2 = Employee::new;
        func2.apply(1002,"tom");
	}

	//数组引用
    //Function中的R apply(T t)
    @Test
    public void test4(){
        Function<Integer, String[]> func1 = length -> new String[length];

        Function<Integer, String[]> func2 = String[] :: new;
        String[] strings = func2.apply(5);
        for (String string : strings) {
            System.out.print(string +" ");
        }
    }
}
